package com.apestech.formwork.xml;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.LocalVariable;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.xml.ReadXmlNodeInter;
import com.apestech.framework.xml.modul.XmlFile;
import com.apestech.framework.xml.XmlSysConfig;
import com.apestech.framework.xml.modul.Xml_File;
import com.apestech.framework.xml.plugin.XmlId;
import com.apestech.framework.xml.plugin.XmlIdVar;
import org.dom4j.Element;
import java.util.ArrayList;

/**
 * 获取配置的查询信息
 *
 * @author hup
 * @create 2018-03-28-17:26
 */
public class XmlQuery extends StringUtil{

    private XmlId xmlid;
    XmlSysConfig config= LocalVariable.getConfig();
    XmlFile xmlfile=null;
    private JSONObject idlist=new JSONObject();
    private SqlSession session;
    private XmlIdVar.STR_TYPE str_type= XmlIdVar.STR_TYPE.XML_STR;
    private String nodename="select";
    private boolean isfile=false;

    public XmlQuery() throws Exception {
        xmlid=new XmlId();
    }

    public XmlQuery(String id) throws Exception {
        xmlid=new XmlId(id);
    }

    public XmlQuery(String id, String nodeName) throws Exception {
        xmlid=new XmlId(id, nodeName);
    }

    public XmlQuery(String str, XmlIdVar.STR_TYPE str_type) throws Exception {
        xmlid=new XmlId(str, str_type);
        this.str_type=str_type;
        if(str_type==XmlIdVar.STR_TYPE.FILE_PATH) isfile=true;
    }

    public void setNodename(String nodename) {
        xmlid.setNodename(nodename);
        this.nodename=nodename;
    }

    public void setTheme(String theme) {
        xmlid.setTheme(theme);
    }

    public void setFile(String file) throws Exception {
        xmlid.setStrInput(file, XmlIdVar.STR_TYPE.FILE_PATH);
    }

    public void setFileDir(String dir) throws Exception {
        xmlid.setStrInput(dir, XmlIdVar.STR_TYPE.DIR_PATH);
    }

    public void setRNode(Element root, Element node){
        xmlid.getXmlfile().setRoot(root);
        xmlid.getXmlfile().setNode(node);
    }

    public Element getNode(String id, String nodename) throws Exception {
        if(nodename==null) nodename=this.nodename;
        else xmlid.getXmlfile().setNodename(nodename);
        Element node=null;
        String sid=id;
        if(id!=null){
            if(sid.indexOf(".")<0){  //当前XML文件中内部结点调用
                if(xmlfile==null) throw new Exception("入口ID ["+id+"] 必须带有结点分割符！");
                sid=xmlfile.getPrefix()+"."+sid;
                copyXmlFile(xmlfile, id, sid, nodename);
            } else {  //跨XML调用
                Xml_File xml_file=config.getXml_File(leftStr(sid, sid.lastIndexOf(".")));
                if(xml_file==null){  //该XML为第一次调用
                    xmlid=new XmlId(sid);
                    xmlid.setNodename(nodename);
                    xmlid.getNode();
                } else {  //XML曾经调用
                    XmlFile xmlf=config.getXmlFile(sid);
                    if(xmlf==null){
                        xmlf=new XmlFile(sid, xml_file);
                        String fid=sid.substring(sid.lastIndexOf(".")+1, sid.length());
                        copyXmlFile(xmlf, fid, sid, nodename);
                    } else
                        xmlid.setXmlfile(xmlf);
                }
            }
        } else {
            xmlid.getNode();
            xmlfile=xmlid.getXmlfile();
        }
        if(xmlfile==null) xmlfile=xmlid.getXmlfile();
        node=xmlid.getXmlfile().getNode();
        idlist.put(xmlid.getXmlfile().getAllid(), xmlid.getXmlfile());
        if(node==null) throw new Exception("id["+xmlid.getXmlfile().getAllid()+"]下没有符合条件的结点，请检查id设置！");
        return node;
    }

    private void copyXmlFile(XmlFile xml, String id, String allid, String nodename) throws Exception {
        XmlFile xmlf=new XmlFile(xml);
        xmlf.setAllid(allid);
        xmlf.setNodename(nodename);
        xmlf.setNode(getRootNode(xml.getRoot(), nodename, id));
        xmlid.setId(id);
        xmlid.setXmlfile(xmlf);
        config.setXmlFile(xmlf);
        idlist.put(allid, xmlf);
    }

    public Element getNode(String id) throws Exception {
        return getNode(id, null);
    }

    public void getXmlFileSingleNode(String param, ReadXmlNodeInter readXmlNodeInter, String nodeName) throws Exception {
        xmlid.getXmlFile(param, readXmlNodeInter, nodeName);
    }

    public void getXmlFileNode(String param, ReadXmlNodeInter readXmlNodeInter) throws Exception {
        xmlid.getXmlFile(param, readXmlNodeInter, null);
    }

    public void getXmlFileNode(ArrayList<String> param, ReadXmlNodeInter readXmlNodeInter) throws Exception {
        xmlid.getXmlFile(param, readXmlNodeInter, null);
    }

    private Element getRootNode(Element root, String nodename, String id){
        if(root==null) return null;
        Element node=(Element) root.selectSingleNode(nodename+"[@id='"+id+"']");
        if(node==null) node=(Element) root.selectSingleNode(id);
        return node;
    }

    public void clearCache() throws Exception {
        String file="";
        ArrayList<String> list=idlist.getKeylist();
        int len=list.size();
        for(int i=0; i<len; i++){
            XmlFile xml= (XmlFile) idlist.get(list.get(i));
            String prefix=xml.getPrefix();
            if((file).indexOf(";"+prefix+";")<0){
                file=file+";"+prefix+";";
                config.removeXmlFile(prefix);
            }
        }
    }

    public Element getRoot() {
        return xmlid.getXmlfile().getRoot();
    }

    public Element getFNode() {
        return xmlid.getXmlfile().getNode();
    }

    public SqlSession getSession() {
        return session;
    }

    public void setSession(SqlSession session) {
        this.session = session;
        if(session!=null) xmlid.setTheme(session.getTheme());
    }

    public void setRoot(Element root){
        xmlid.getXmlfile().setRoot(root);
    }
}
